from google.appengine.ext import db
import datetime

class Checkout(db.Model):
  """ GData services allow us to "check out" a document to a user
  
  This lets the user use Google Docs to edit data, providing a decent
  user experience with little work on our end
  
  """
  # Who is editing the data
  checked_out_to = db.UserProperty()
  
  # Key Value for this Google Doc
  gdoc_key = db.StringProperty()
  
  # What order will the fields be in?
  # Denote in "fieldset_key#field_name" format
  fields_list = db.StringListProperty()
  
  # Times
  checked_out_on = db.DateTimeProperty(auto_now_add=True)
  checked_in = db.BooleanProperty(default=False)
  checked_in_on = db.DateTimeProperty()
  
  @property
  def user_link(self):
    """ URL that takes the user to the spreadsheet """
    if self.gdoc_key:
      return 'http://spreadsheets.google.com/ccc?key=' + self.gdoc_key
    return None
  
  def check_in(self):
    self.checked_in = True
    self.checked_in_on = datetime.datetime.now()
    self.put()

